package com.nbjtjc.safe.service.edu;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.edu.EduRequirementSurveyItemMapper;
import com.nbjtjc.safe.model.edu.EduRequirementSurveyItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EduRequirementSurveyItemService{
	@Autowired
	  private EduRequirementSurveyItemMapper eduRequirementSurveyItemMapper;
		
	  public Result add(EduRequirementSurveyItem eduRequirementSurveyItem){
		        Result result=new Result();
		        if(ObjectUtil.isNull(eduRequirementSurveyItem)
//		        		||ObjectUtil.isNull(eduRequirementSurveyItem.getId())
		        		||ObjectUtil.isNull(eduRequirementSurveyItem.getReqInvestId())
		        		||ObjectUtil.isNull(eduRequirementSurveyItem.getItems())
		        		){
		            result.setFalse("关键字段不能为空");
		            return result;
		        }

		        if(ObjectUtil.isNull(eduRequirementSurveyItem.getId())
		        		|| ObjectUtil.isNull(eduRequirementSurveyItemMapper.selectByPrimaryKey(eduRequirementSurveyItem.getId()))){
		        	
		        	int res=eduRequirementSurveyItemMapper.insertSelective(eduRequirementSurveyItem);
		            
		        if(res==0){
		            result.setFalse("插入失败");
		            return result;
		        }
		        }else
		        {
		        	int res=eduRequirementSurveyItemMapper.updateByPrimaryKeySelective(eduRequirementSurveyItem);
		        	 if(res==0){
		        		 result.setFalse("更新失败");
		        		 return result;
		        	 }
		        }
		        result.setData(eduRequirementSurveyItem);

		        return result;
		    }


	  
	  public Result delete(int eduRequirementSurveyItemID){
	      Result result=new Result();
	      if(ObjectUtil.isNull(eduRequirementSurveyItemID)){
	          result.setFalse("删除对象不能为空");
	          return result;
	      }
	      int res = eduRequirementSurveyItemMapper.deleteByPrimaryKey(eduRequirementSurveyItemID);
     	 if(res==0){
    		 result.setFalse("删除失败");
    		 return result;
    	 }
	      return result;
	  }
	  
//	  根据需求调查ID查找调查项目
	  
	  public Result find(EduRequirementSurveyItem eduRequirementSurveyItem) {
	      Result result = new Result();
	      PageHelper.startPage(eduRequirementSurveyItem.getPageCurrent(), eduRequirementSurveyItem.getPageSize());
	      List<EduRequirementSurveyItem> eduRequirementSurveyItems = eduRequirementSurveyItemMapper.select(eduRequirementSurveyItem);
	      result.setData(new PageInfo<>(eduRequirementSurveyItems));

	      return result;
	  }


}
